<?php

namespace support\login;

use app\services\SmsService;
use app\services\UserService;
use plugin\admin\app\model\User;

class CaptchaLogin implements LoginContract
{

    public function check($data)
    {
        if (!$data['mobile']) {
            throw new \Exception('手机号不能为空');
        }

        $user = User::where('mobile', $data['mobile'])->first();

        if (empty($data['captcha'])) {
            throw new \Exception('验证码不能为空');
        }

        SmsService::check($data['mobile'], SmsService::LOGIN, $data['captcha']);

        if (!$user) {
//            if (empty($data['password'])) {
//                throw new \Exception('密码不能为空');
//            }
            $userId = UserService::register($data);
            $user = UserService::profile($userId);
        }

        return UserService::profile($user['id']);
    }
}